package com.ql.service.menzen;

import com.ql.dao.menzhen.UserMapper;
import com.ql.pojo.menzhen.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userLoginService")
public class UserLoginServiceImpl implements UserLoginService {

    private static final Logger LOGGER = Logger.getLogger(UserLoginServiceImpl.class);

   @Resource
   public UserMapper userMapper;

    @Override
    public boolean saveUser(User user) {
        boolean flag = false;

        try {
            int row = userMapper.addUser(user);
            if(row != 1){
                LOGGER.error("UserLoginServiceImpl.saveUser 失败");
            }else {
                LOGGER.error("UserLoginServiceImpl.saveUser 成功");
                flag = true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("UserLoginServiceImpl.saveUser 异常:"+e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean doLogin(String userCode, String pwd) {
        boolean flag = false;
        try {
            User user = userMapper.qryByUserCode(userCode);
            if (null == user) {
                LOGGER.debug("UserLoginServiceImpl.doLogin 查询用户为空!");
            } else {
                if (!pwd.equals(user.getUserPassword())) {
                    LOGGER.debug("UserLoginServiceImpl.doLogin 输入的账号和密码不匹配!");
                } else {
                    LOGGER.debug("UserLoginServiceImpl.doLogin 登陆成功>>>>>!");
                    flag = true;
                }
            }
        } catch (Exception e) {
            LOGGER.error("UserLoginServiceImpl.doLogin 异常:" + e.getMessage());
        }

        return flag;
    }

    @Override
    public User getUserByCode(String userCode) {
        User user = null;

        try {
            user = userMapper.qryByUserCode(userCode);
            if(user == null){
                LOGGER.debug("UserLoginServiceImpl.getUserByCode 查询为空==");
            }else{
                LOGGER.debug("UserLoginServiceImpl.getUserByCode 查询成功！");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("UserLoginServiceImpl.getUserByCode 查询异常"+e.getMessage());
        }
        return user;
    }

    @Override
    public User getUser(int userId) {
        User user = null;

        try {
            user = userMapper.qryByUser(userId);
            if(user == null){
                LOGGER.debug("UserLoginServiceImpl.getUser 查询失败！");
            }else {
                LOGGER.debug("UserLoginServiceImpl.getUser 查询成功！");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("UserLoginServiceImpl.getUser 查询异常"+e.getMessage());
        }

        return user;
    }
}
